const fileService = require("../service/file.service")
const { APP_HOST, APP_PORT } = require('../app/config')
const userService = require('../service/user.service')

class FileController {
    async created(ctx, next) {
        // 1.获取上传文件的数据
        console.log(ctx.request.file)
        const { filename, mimetype, size } = ctx.request.file
        const { id } = ctx.user
        // 2.操作数据库
        const result = await fileService.created(filename, mimetype, size, id)

        const avatarUrl = `${APP_HOST}:${APP_PORT}/users/avatar/${id}`
        // 把头像的信息，保存在url表中
        const result2 = await userService.updateUserAvatar(avatarUrl, id)
        // console.log(result)
        ctx.body = {
            code: 0,
            message: '头像上传成功~~',
            data: avatarUrl
        }
    }

}
module.exports = new FileController() 